package com.example.anni.zhuzhoujia.tianya12345;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextPaint;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.anni.zhuzhoujia.R;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.indicator.ScrollIndicatorView;
import com.shizhefei.view.indicator.slidebar.ColorBar;
import com.shizhefei.view.indicator.transition.OnTransitionTextListener;

/**
 * Created by yangshengwen2016 on 2016/6/13.
 * 我的同城活动
 */
public class CityMyActivity extends AppCompatActivity {
    private ViewPager mViewPager;
    private ScrollIndicatorView mScrollIndicatorView;
    private IndicatorViewPager mIndicatorViewPager;
    private String[] titles = {"我发起的活动", "我参与的活动"};
    private Fragment[] fragments = {
            new CityFragmentMyPublish(),
            new CityFragmentMyParticipateIn(),
    };

    /*//返回键触发的事件
    @Override
    protected void onPause() {
        super.onPause();
        finish();
        overridePendingTransition(0, R.anim.ysw_b_out);
    }*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.city_my_activity);
        LinearLayout city_return = (LinearLayout) findViewById(R.id.city_return);//返回图片
        city_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(0, R.anim.ysw_b_out);
            }
        });
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("我的活动");//设置标题
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        mScrollIndicatorView = (ScrollIndicatorView) findViewById(R.id.indicator);

        mScrollIndicatorView.setScrollBar(new ColorBar(CityMyActivity.this, Color.RED, 2));

        mIndicatorViewPager = new IndicatorViewPager(
                mScrollIndicatorView, mViewPager);
        NewsAdapter adapter = new NewsAdapter(getSupportFragmentManager());
        mIndicatorViewPager.setAdapter(adapter);

        //添加线条指定器
        mScrollIndicatorView.setScrollBar(new ColorBar(CityMyActivity.this, Color.RED, 5));
        //改变文字的颜色
        mScrollIndicatorView.setOnTransitionListener(new OnTransitionTextListener()
                .setColorId(CityMyActivity.this, R.color.colorAccent, R.color.black));
    }

    class NewsAdapter extends IndicatorViewPager.IndicatorFragmentPagerAdapter {

        public NewsAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public View getViewForTab(int i, View view, ViewGroup viewGroup) {
            //设置上选项卡标题
            TextView tvLabel = new TextView(CityMyActivity.this);
            tvLabel.setText(titles[i]);
            TextPaint tp = tvLabel.getPaint();
            tp.setFakeBoldText(true);
            tvLabel.setTextColor(0xFF000000);
            tvLabel.setBackgroundColor(0xFFffffff);
            tvLabel.setPadding(20, 50, 20, 50);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            tvLabel.setLayoutParams(lp);
            tvLabel.setGravity(Gravity.CENTER);
            return tvLabel;
        }

        @Override
        public Fragment getFragmentForPage(int i) {
            return fragments[i];
        }
    }
}
